package com.bobi.tfca.ui.mainnavigation.user.quit

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.navigation.NavOptions
import androidx.navigation.findNavController
import com.bobi.tfca.R
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.constants.ReplyConstant
import com.bobi.tfca.databinding.FragmentQuitAssBinding
import com.bobi.tfca.network.ApiResponse
import com.bobi.tfca.network.api.AuditAPI
import com.bobi.tfca.network.api.MemberAPI
import com.bobi.tfca.util.LogUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import okhttp3.MediaType
import okhttp3.RequestBody

class QuitAssFragment: BaseFragment<FragmentQuitAssBinding, ViewModel>(
    FragmentQuitAssBinding::inflate,
    null,
    true
) {

    @SuppressLint("ResourceType", "UseRequireInsteadOfGet")
    override fun initFragment(
        binding: FragmentQuitAssBinding,
        viewModel: ViewModel?,
        savedInstanceState: Bundle?
    ) {
        // 设置topPanel
        setTopPanelStyle(binding)
        // 设置退出协会申请监听
        quitAss(binding)

    }

    private fun quitAss(binding: FragmentQuitAssBinding) {
        binding.btConfirm.setOnClickListener {
            // 创建确认框
            val builder = AlertDialog.Builder(requireContext())
            builder.setTitle("退出")
            builder.setMessage("你确认退出协会吗")

            // 设置确认按钮和点击事件
            builder.setPositiveButton("确认") { dialog, which ->
                confirmQuit(binding)
            }

            // 设置取消按钮和点击事件
            builder.setNegativeButton("取消") { dialog, which ->
            }

            // 创建并显示确认框
            val dialog: AlertDialog = builder.create()
            dialog.show()
        }
    }

    private fun confirmQuit(binding: FragmentQuitAssBinding) {
        // 退出协会
        val leaveWords = binding.etWords.text.toString()
        val requestBody = RequestBody.create(MediaType.parse("text/plain"), leaveWords)
        publicViewModel!!.apply {
            request(AuditAPI::class.java).applyQuitAss(requestBody).getResponse {
                it.collect {
                    when (it) {
                        is ApiResponse.Error -> {
                            LogUtil.e(it.errMsg)
                        }

                        ApiResponse.Loading -> LogUtil.e("Loading")
                        is ApiResponse.Success -> {
                            withContext(Dispatchers.Main) {
                                val res = it.data
                                if (res?.code == 200) {
                                    // 重置用途和用户和密码
                                    publicViewModel!!.reply = ReplyConstant.QUIT_ASS_REPLY
                                    // 跳转回复界面
                                    requireActivity().findNavController(R.id.app_nav).navigate(
                                        R.id.action_quitAssFragment_to_replyFragment, null,
                                        NavOptions.Builder().setPopUpTo(R.id.quitAssFragment, true)
                                            .build()
                                    )
                                } else {
                                    Toast.makeText(
                                        requireActivity(),
                                        "操作失败",
                                        Toast.LENGTH_SHORT
                                    ).show()
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    /**
     * 设置顶部面板的样式
     */
    private fun setTopPanelStyle(binding: FragmentQuitAssBinding) {
        val topPanel = binding.topPanel
        topPanel.leftImage.setOnClickListener {
            requireActivity().findNavController(R.id.app_nav).navigateUp()
        }
        val rightLayout = topPanel.rightLayout
        rightLayout.visibility = View.GONE
        topPanel.titleText.visibility = View.GONE
    }

}